package org.road0329.dlt698.axdr;


import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrSequenceOf;

public class GetResponseRecordList {
    private byte[] dataCode = null;
    private PIID piid_acd = null;
    private SubSeqOfRecordResults results = null;

    public GetResponseRecordList() {
    }

    public void set_dataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void set_all(PIID piid_acd, SubSeqOfRecordResults results) {
        this.piid_acd = piid_acd;
        this.results = results;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.piid_acd = new PIID();
        this.piid_acd.setNumBit(8);
        codeLength += this.piid_acd.decode(input);
        this.results = new SubSeqOfRecordResults();
        codeLength += this.results.decode(input);
        return codeLength;
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength = 0;
        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if (this.piid_acd != null && this.results != null) {
                codeLength += this.results.encode(output);
                codeLength += this.piid_acd.encode(output);
            } else {
                throw new RuntimeException("results or piid_acd is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.toByteArray();
    }

    @Override
    public String toString() {
        if (this.piid_acd != null && this.results != null) {
            return "sequence: {" + "piid_acd: " + this.piid_acd + ", results: " + this.results + "}";
        } else {
            return "piid_acd || results is null";
        }
    }

    class SubSeqOfRecordResults extends AxdrSequenceOf<AResultRecord> {
        @Override
        public AResultRecord createListElement() {
            return new AResultRecord();
        }
    }
}
